/*
 *	main-view view
 *  -----------------------------------------------------------------------------------------------------------
 */

site.view('main-view', {
	
	onLoad: function(settings) {
		this.startAll(settings);
	},
	
	onUnload: function() {
		this.stopAll();
	}

})

/*
 *	rain-animation
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('rain-animation', function(context) {
	var query = context.query,
		request = context.request,
		images, 

		start_animate = function() {
 			
 			var icon_matrix =  context.query('.main_page_icon_matrix'),
 				was_called = false;
	        
	        icon_matrix.animate({'top':'+=300'}, 10000, 'linear',function(){
	           	
	           	icon_matrix.css('top','-=300');
	            
	            if(!was_called) {
	                was_called = true;   

	                create_image_container(images, 1);
	                
	                $('.main_page_icon_matrix:last').remove();
	                
	                start_animate();
	            }
	        });
    	},

    	create_image_container = function(images_array, quantity) {
    		var matrix = [],
        		random_value = 0,
        		main_page_desktop = context.query('#main_page_desktop'),
        		main_page_icon_matrix,
        		img;

        	
        	for(var q = 0; q < quantity; q++) {	
	        	main_page_icon_matrix = context.createElement('div');
				main_page_icon_matrix.setAttribute('class', 'main_page_icon_matrix');
	        
		        for (var i = 0; i < 6; i++) {
		        	matrix[i] = [];

		            for (var j = 0; j < 5; j++) {
		            
		                random_value = (Math.floor(Math.random()*24));

		                if (matrix[i][random_value] != 1) {
		                    
		                    matrix[i][random_value] = 1;
		                    img = context.createElement('img');
		                    img.setAttribute('src', images_array[(Math.floor(Math.random() * images_array.length))]);
		                    img.setAttribute('style', 'top: ' +  (i * 50 + 9) + 'px; right: ' + (random_value * 50 + 9) + 'px');
		                    main_page_icon_matrix.appendChild(img);

		                }
		            }
		        }

		        main_page_desktop.prepend(main_page_icon_matrix);
	    	}
    	}

	return {
		init: function(settings) {
			request({
					url: '/get_animation_images',
					type: 'post',
					dataType: 'json',
					success: function(data) {
						images = data.content;
						create_image_container(images, 3);
						start_animate();
					}
			});
		}
	}
})

/*
 *	login-form
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('login-form', function(context) {
	var util = context.util,
		require = context.require,
		ajaxSubmitForm = require('ajaxSubmitForm'),		
  		notify = require('notify'),
		body = context.query('body'),
		content;

	return {
		init: function() {
			ajaxSubmitForm.init('#login_form',{


				success: function(data) {
					notify.create("create", {
						title: data.message,
						text: data.description
					});  

					setTimeout(function() {
						body.fadeOut();
						content = data.content;
						setTimeout(function(){			                  
							body.html(content).fadeIn();
							context.setView('main-login-view');			                        
						}, 500);
					}, 1500);

				},

				error: function(data) {
					var err_msg = '';

					util.eachRecursive(data.errors, function(key, val) {
						err_msg += err_msg === '' ? val : '<br/>' + val;
					})


					notify.create('create','error-template', {
						title: data.message,
						text: err_msg
					});
				}
			});
		}
	}
})



/*
 *	recover-form
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('recover-form', function(context) {
	var util = context.util,
		require = context.require,
		ajaxSubmitForm = require('ajaxSubmitForm'),
  		notify = require('notify');

	return {
		init: function() {
			ajaxSubmitForm.init('#recover_password_form',{


				success: function(data) {
					notify.create("create", {
						title: data.message,
						text: data.description
					});  

					this[0].reset();
				},

				error: function(data) {
					var err_msg = '';

					util.eachRecursive(data.errors, function(key, val) {
						err_msg += err_msg === '' ? val : '<br/>' + val;
					})

					notify.create('create','error-template', {
						title: data.message,
						text: err_msg
					});
				}
			});
		}
	}
})


/*
 *	login-recover-switcher
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('login-recover-switcher', function(context) {
	var panels = context.query('#main_page_forms div'),
		login_form = context.query('#login_form'),
		recover_password_form = context.query('#recover_password_form')
		position = true;

	return {
		init: function() {    
    		panels.find('a').click(function() {
		        if (position) {

		             panels.animate({top: "-164px"}, 500, function() { 
		             	position = false; 
		             	login_form[0].reset();
		             	login_form.find('#login, #password').removeAttr('style');
		             });

		      	} else {

		             panels.animate({top: "0px"}, 500, function() { 
		             	position = true;
		             	recover_password_form[0].reset();
		             	recover_password_form.find('#e_mail').removeAttr('style'); 
		             });
		        }      
    		});
		}
	}
})


/*
 *	info-slider
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('info-slider', function(context) {
	var slide_number = 0,
		interval,
		interval_timer = 10000,		
		slides = context.query('#main_page_navigation').find('li'),
		slides_content = context.query('#main_page_content').find('div'),
		slides_amount = slides.length,

		slider = function() {

			slides[slide_number].setAttribute('class','');
			slides_content[slide_number].setAttribute('class','');

			slide_number++;
			if (slide_number === slides_amount) slide_number = 0;
           		 
        	slides[slide_number].setAttribute('class','active'); 
        	slides_content[slide_number].setAttribute('class','active');        

		}

	return {
		init: function() {

			interval = setInterval(slider, interval_timer);
			slides.on('click', function(e) {
				clearInterval(interval);

				slides[slide_number].setAttribute('class', '');
				slides_content[slide_number].setAttribute('class', '');

				slides[e.currentTarget.getAttribute('rel')].setAttribute('class','active');
				slides_content[e.currentTarget.getAttribute('rel')].setAttribute('class','active');  
           		slide_number = e.currentTarget.getAttribute('rel');

				interval = setInterval(slider, interval_timer);
			})
		}
	}
})

